package com.shiftychess.chess_graphics{
	
	import flash.events.Event;
	
	import mx.core.UIComponent;

	public class Grid extends UIComponent{

		protected var _square_size:uint=50;
		protected var _square_size_changed:Boolean=false;
		public function set square_size(s:uint):void{
			_square_size=s;
			_square_size_changed=true;
			invalidateProperties();
		}
		public function get square_size():uint{return _square_size;}
		
		protected var _transparency_changed:Boolean=false;
		protected var _transparency:Number=.04;
		public function set transparency(t:Number):void{
			_transparency=t;
			_transparency_changed=true;
			invalidateProperties();
		}
		
		override protected function commitProperties():void{
			if(_transparency_changed){
				_transparency_changed=false;
				this.invalidateDisplayList();
			}
			if(_square_size){
				_square_size_changed=false;
				this.invalidateDisplayList();
			}
		}
		
		override protected function updateDisplayList(unscaled_width:Number,unscaled_height:Number):void{
			render();
		}
		

		//constructor
		public function Grid(){
			super();
		}
		
		
		public function render(e:Event=null):void{
			graphics.clear();
			var is_white:Boolean=true;
			var start_white:Boolean=true;
			for(var i:uint=0;i<8;i++){
				is_white=start_white;
				for(var j:uint=0;j<8;j++){
					graphics.beginFill(is_white ? 0xFFFFFF : 0x000000, _transparency);
					graphics.drawRect(j*_square_size,i*_square_size,_square_size,_square_size);
					graphics.endFill();
					is_white=!is_white;
				}
				start_white=!start_white;
			}
		}
		
		
		
	}
}